//
//  LXSignPayRentPlanTableViewCell.m
//  ZuLinProject
//
//  Created by bancheng on 2022/9/14.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXSignPayRentPlanTableViewCell.h"
#import "LXSignContractModel.h"
@interface LXSignPayRentPlanTableViewCell ()
@property (nonatomic)NSMutableArray *data;

@end


@implementation LXSignPayRentPlanTableViewCell


int height = 40;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier WithData:(nonnull NSMutableArray *)data WithSignType:(NSInteger)signType IsShow:(BOOL)isShow{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.data = data;

        
        
        [self.contentView addSubview:self.titleLab];
        
        
        [self.contentView addSubview:self.contentLab];
        
        NSArray *ary = @[@"类型",@"应付日期",@"金额",@"备注",@"操作"];
        
        CGFloat topX = 0;
        
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(15, self.titleLab.Y_height + 10, ScreenWidth - 30 , height)];
        topView.backgroundColor = ColorWithF5F5F5;
        [self.contentView addSubview:topView];
        for (int i = 0; i < ary.count; i++) {
    
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(topX, 0, topView.width/ary.count, topView.height)];
            label.textColor = ColorWith333333;
            label.font = [UIFont systemFontOfSize:15];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = ary[i];
            [topView addSubview:label];
            
            topX = label.X_width;
        }
        
        
        CGFloat X = 15;
        CGFloat Y = topView.Y_height;
        CGFloat W = topView.width/ary.count;
        
        NSString *totalRent = @"0.0";
        NSString *totalDepost = @"0.0";
        
        BOOL isNull = NO;
        
        if (data.count == 0) {
            isNull = YES;
            NSArray *ary = @[
                
                @{
                    @"fundType":signType==1?@(1):@(2),
                    @"exceptReceiveDate":@"--",
                    @"amount":@"--",
                    @"remark":@"--",
                    @"period":@"",
                },
                @{
                    @"fundType":signType==1?@(0):@(3),
                    @"exceptReceiveDate":@"--",
                    @"amount":@"--",
                    @"remark":@"--",
                    @"period":@"",
                },
            ];
            data = [LXSignContractModel mj_objectArrayWithKeyValuesArray:ary];
        }
        
        

        
        
        
        for (int i = 0; i < data.count; i++) {
            
            LXSignContractModel *model = data[i];
            
            if (model.fundType == 0 || model.fundType == 3) {
                totalRent = [NSString calculateByadding:model.amount secondNumber:totalRent];
            }
            else {
                totalDepost = [NSString calculateByadding:model.amount secondNumber:totalRent];
            }
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(X, Y, topView.width , height)];
            [self.contentView addSubview:view];
            [view addLeftBorderWithColor:[UIColor colorWithHexString:@"#F2F2F2"] andWidth:1];
            [view addRightBorderWithColor:[UIColor colorWithHexString:@"#F2F2F2"] andWidth:1];
            [view addBottomBorderWithColor:[UIColor colorWithHexString:@"#F2F2F2"] andWidth:1];

            
    
            UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, W, view.height)];
            label1.textColor = ColorWith666666;
            label1.textAlignment = NSTextAlignmentCenter;
            label1.font = Fond13;
            [view addSubview:label1];
            /**款项类别 0租金 1租车押金 2购车首付,3购车月供,4购车尾款, 5平台返佣 6保险赔付*/
            label1.text = [NSString getCollectionNameWithType:model.fundType];
            
            UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(label1.X_width, 0, W, view.height)];
            label2.textColor = ColorWith666666;
            label2.font = Fond12;
            label2.adjustsFontSizeToFitWidth = YES;
//            label2.numberOfLines = 2;
            label2.textAlignment = NSTextAlignmentCenter;
            label2.text = model.exceptReceiveDate;
            [view addSubview:label2];
            
            UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(label2.X_width, 0, W, view.height)];
            label3.textColor = ColorWith666666;
            label3.font = Fond13;
            label3.textAlignment = NSTextAlignmentCenter;
            label3.text = model.amount;
            [view addSubview:label3];
            
            UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(label3.X_width, 0, W, view.height)];
            label4.textColor = ColorWith666666;
            label4.font = Fond13;
            label4.adjustsFontSizeToFitWidth = YES;
            label4.textAlignment = NSTextAlignmentCenter;
            label4.text = model.remark;
            [view addSubview:label4];
            
            UIButton *label5 = [[UIButton alloc] initWithFrame:CGRectMake(label4.X_width, 0, W, view.height)];
            label5.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            if (isNull || i == 0 || isShow) {
                [label5 setImage:[UIImage imageNamed:@"car_information_edit_ash"] forState:UIControlStateNormal];
            }
            else {
                [label5 setImage:[UIImage imageNamed:@"carDetail_修改信息"] forState:UIControlStateNormal];
                [label5 whenTapped:^{
                                    
                    if (self.clickBtnAction) {
                        self.clickBtnAction(model);
                    }
                    
                    
                    
                    
                }];
            }
            
            
            [view addSubview:label5];

            Y = view.Y_height;
        }
        
        
        NSString *title1 = signType==1?@"租金":@"月供";
        NSString *title2 = @"0";
        NSString *title3 = @"0";
        NSString *title4 = signType==1?@"押金":@"首付";
        NSString *title5 = @"0";
        NSString *title6 = @"0";
        
        
        if (!isNull) {
            title2 = totalRent;
            title3 = [NSString stringWithFormat:@"%@",@(data.count - 1)];
            title5 = totalDepost;
            title6 = [NSString calculateByadding:totalRent secondNumber:totalDepost];
        }
       
        self.contentLab.text = [NSString stringWithFormat:@"%@共%@元(%@期)；%@共%@元；总计%@元",title1,title2,title3,title4,title5,title6];

       
        self.contentLab.Y = Y + 10;

    }
    return self;
    
    
    
}
- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 18, 200, 18)];
        _titleLab.textColor = ColorWith333333;
        _titleLab.font = Fond16;
    }
    return _titleLab;
}
- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] initWithFrame:CGRectMake(15, self.height - 28, ScreenWidth - 30, 18)];
        _contentLab.textColor = ColorWith333333;
        _contentLab.adjustsFontSizeToFitWidth = YES;
        _contentLab.font = Fond14;
    }
    return _contentLab;
}
+ (CGFloat)getHeight:(NSArray *)ary{
    if (ary.count == 0) {
        return 205;
    }
    else {
        return ary.count * height  + 43 + 42 + height;
    }
}
@end
